/*
 * Created on Jan 4, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.event;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.scohen.juploadr.app.UploadImage;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class MonitorFilePart extends FilePart {
    private List<UploadStatusMonitor> listeners;
    private File uploadFile;
    private UploadImage image;

    /**
     * @param arg0
     * @param arg1
     * @throws java.io.FileNotFoundException
     */
    public MonitorFilePart(String arg0, File arg1, UploadImage img)
            throws FileNotFoundException {
        super(arg0, arg1);
        uploadFile = arg1;
        this.image = img;
    }

    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     * @throws java.io.FileNotFoundException
     */
    public MonitorFilePart(String arg0, File arg1, String arg2, String arg3,
            UploadImage img) throws FileNotFoundException {
        super(arg0, arg1, arg2, arg3);
        uploadFile = arg1;
        this.image = img;
    }

    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @throws java.io.FileNotFoundException
     */
    public MonitorFilePart(String arg0, String arg1, File arg2, UploadImage img)
            throws FileNotFoundException {
        super(arg0, arg1, arg2);
        this.image = img;
    }

    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     * @param arg4
     * @throws java.io.FileNotFoundException
     */
    public MonitorFilePart(String arg0, String arg1, File arg2, String arg3,
            String arg4, UploadImage img) throws FileNotFoundException {
        super(arg0, arg1, arg2, arg3, arg4);
        uploadFile = arg2;
        this.image = img;
    }

    /**
     * @param arg0
     * @param arg1
     */
    public MonitorFilePart(String arg0, PartSource arg1, UploadImage img) {
        super(arg0, arg1);
        uploadFile = new File(arg1.getFileName());
        this.image = img;
    }

    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    public MonitorFilePart(String arg0, PartSource arg1, String arg2,
            String arg3, UploadImage img) {
        super(arg0, arg1, arg2, arg3);
        uploadFile = new File(arg1.getFileName());
        this.image = img;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.commons.httpclient.methods.multipart.Part#sendData(java.io.OutputStream)
     */
    protected void sendData(OutputStream out) throws IOException {

        EventOutputStream eventOut = new EventOutputStream(out, image, 1);
        eventOut.setFileName(uploadFile.getAbsolutePath());
        for (int i = 0; i < listeners.size(); i++) {
            UploadStatusMonitor listener = (UploadStatusMonitor) listeners
                    .get(i);
            eventOut.addUploadStatusMonitor(listener);
        }
        super.sendData(eventOut);
    }

    public void addStateChangeListener(UploadStatusMonitor toAdd) {
        if (listeners == null) {
            listeners = new LinkedList<UploadStatusMonitor>();
        }
        listeners.add(toAdd);
    }

    public UploadImage getImage() {
        return image;
    }

    public void setImage(UploadImage image) {
        this.image = image;
    }
}